﻿using System;
using NetJsWire.Direct.attributes;
using NetJsWire.JsonRpcHandler;
using Newtonsoft.Json.Linq;

namespace AdministracionGlobalUI2.Direct
{
    [DirectAction]
    public partial class Methods
    {
        [DirectMethod( ParameterHandling = ParameterHandling.AutoResolve )]
        public static string LoadWindow( string window, bool show, bool modal = false, object[] parameters = null )
        {
            var vent = Activator.CreateInstance( Type.GetType( window, true, true ), parameters );
            string toReturn = null;
            if ( vent is NetJsWire.Lite.Widgets.UI.Window )
            {
                using ( var win = ( vent as NetJsWire.Lite.Widgets.UI.Window ) )
                {
                    if ( show )
                    {
                        win.InitHidden = false;
                        win.Hidden = false;
                    }
                    if ( modal )
                        win.Modal = true;

                    toReturn = win.Serialize( Newtonsoft.Json.Formatting.None );
                }
            }
            
            if ( vent is NetJsWire.Widgets.Ui.Window.Window )
            {
                using ( var win = ( vent as NetJsWire.Widgets.Ui.Window.Window ) )
                {
                    if ( modal )
                        win.Modal = true;
                    if ( show )
                        win.Show();

                    toReturn = WebApplication.Instance.RenderChanges();
                }
            }

            return toReturn;
        }

        public static string OpenSystemWindow( string url, string title = "", int width = 600, int height = 400 )
        {
            return string.Format( "NetJsWire.SystemWindow('{0}', '{1}', {2}, {3});", url, title, width, height );
        }
    }
}
